﻿using hospital_registration_system.Entity;
using LinkSql;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace hospital_registration_system.DAL
{
    public class Illness_departmentsDao
    {
        MySqlDataAdapter mySqlDataAdapter;
        private string tableName = "illness_departments";
        private string illness_id = "illness_id";
        private string department_id = "department_id";
        private string CREATED_BY= "CREATED_BY";//创建人
        private string CREATED_TIME= "CREATED_TIME";//创建时间
        private string UPDATED_BY= "UPDATED_BY";//更新人
        private string UPDATED_TIME= "UPDATED_TIME";//更新时间
        private string state= "state";//状态


        /// <summary>
        /// 通过对象插入
        /// </summary>
        /// <param name="illdep"></param>
        /// <returns></returns>
        public bool insert(Illness_departments illdep)
        {
            Dictionary<string, object> map = new Dictionary<string, object>();
            map.Add(this.illness_id, illdep.Illness_id);
            map.Add(this.department_id, illdep.Department_id);
            map.Add(this.CREATED_BY, illdep.CREATED_BY1);
            map.Add(this.CREATED_TIME, illdep.CREATED_TIME1);
            map.Add(this.UPDATED_BY, illdep.UPDATED_BY1);
            map.Add(this.UPDATED_TIME, illdep.UPDATED_TIME1);
            map.Add(this.state, illdep.State);
            return MySQLUtils.insert(tableName, map);
        }


        /// <summary>
        /// 插入疾病科室对应信息,建议使用对象插入
        /// </summary>
        /// <param name="illness_id">疾病id</param>
        /// <param name="department_id">科室id</param>
        /// <returns></returns>
        public bool insert(int illness_id,int department_id)
        {
            Dictionary<string, object> map = new Dictionary<string, object>();
            map.Add(this.illness_id, illness_id);
            map.Add(this.department_id, department_id);
            return MySQLUtils.insert(tableName, map);
        }

        /// <summary>
        /// 通过illness_id查询
        /// </summary>
        /// <param name="illness_id">疾病id</param>
        /// <returns></returns>
        public List<Illness_departments> selectByIllnessId(int illness_id)
        {
            List<Illness_departments> ret = new List<Illness_departments>();
            Dictionary<string, object> map = new Dictionary<string, object>();
            map.Add(this.illness_id,illness_id);
            return GetIllness_DepartmentsByDataTable(MySQLUtils.selectByMap(tableName,map));
            
        }

        /// <summary>
        /// 通过department_id查询
        /// </summary>
        /// <param name="department_id"></param>
        /// <returns></returns>
        public List<Illness_departments> selectByDepartmentId(int department_id)
        {
            List<Illness_departments> ret = new List<Illness_departments>();
            Dictionary<string, object> map = new Dictionary<string, object>();
            map.Add(this.department_id, department_id);
            return GetIllness_DepartmentsByDataTable(MySQLUtils.selectByMap(tableName, map));
            
        }

        /// <summary>
        /// 通过id删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool delete(int id)
        {
            return MySQLUtils.delete(tableName, id);
        }

        /// <summary>
        /// 通过map条件修改
        /// </summary>
        /// <param name="map"></param>
        /// <returns></returns>
        public bool updateByMap(Dictionary<string,Object> map)
        {
            return MySQLUtils.update(tableName, map);
        }

        /// <summary>
        /// 将DtatTable转化为List
        /// </summary>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        public List<Illness_departments> GetIllness_DepartmentsByDataTable(DataTable dataTable)
        {
            List<Illness_departments> ret = new List<Illness_departments>();
            foreach (DataRow row in dataTable.Rows)
            {
                Illness_departments one = new Illness_departments(
                    int.Parse(row["id"].ToString()),
                    int.Parse(row["illness_id"].ToString()),
                    int.Parse(row["department_id"].ToString()),
                    row["created_by"].ToString(),
                    (DateTime)(row["created_time"]),
                    row["updated_by"].ToString(),
                    (DateTime)(row["updated_time"]),
                    int.Parse(row["state"].ToString())
                    );
                ret.Add(one);
            }
            return ret;
        }
    }
}
